package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.service.AdminBrandService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("admin/brand")
public class AdminBrandController {

    @Autowired
    private AdminBrandService adminBrandService;
    @Autowired
    SecurityManager securityManager;

    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商管理"},button = "查询")
    @GetMapping("list")
    public Object list(Integer page, Integer limit, String sort, String order, Integer id, String name) {

        List<MarketBrand> brandList = adminBrandService.list(page, limit, sort, order, id, name);
        Object o = ResponseUtil.okList(brandList);
        return o;
    }

    @PostMapping("delete")
    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商管理"},button = "删除")
    public Object delete(@RequestBody Map params) {
        Integer id = (Integer) params.get("id");
        adminBrandService.delete(id);
        Object ok = ResponseUtil.ok();
        return ok;
    }


    @PostMapping("update")
    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商管理"},button = "编辑")
    public Object update(@RequestBody MarketBrand marketBrand) {
        //读取请求体中内容
        int i=adminBrandService.update(marketBrand);
        Object o = ResponseUtil.ok(i);
        return o;
    }


    @PostMapping("create")
    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商管理"},button = "添加")
    public Object create(@RequestBody Map params) {
        String  desc = (String) params.get("desc");
        BigDecimal floorPrice = (BigDecimal) params.get("floorPrice");
        String name = (String) params.get("name");
        String picUrl = (String) params.get("picUrl");
        String s = floorPrice.toString();
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(desc) || floorPrice != null) {
            Object o = ResponseUtil.fail(401, "参数不对");
            return o;
        }else if (!s.matches("\\d+")){
            Object o = ResponseUtil.badArgument();
            return o;
        }

        MarketBrand marketBrand = adminBrandService.create(name, desc, picUrl, floorPrice);

        //获取当前时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String addTimeToString = marketBrand.getAddTime().format(formatter);
        String updateTimeToString = marketBrand.getUpdateTime().format(formatter);

        SecurityUtils.setSecurityManager(securityManager);
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("brand",marketBrand);

        Map<String,Object> data = new HashMap<>();
        data.put("id", marketBrand.getId());
        data.put("name", marketBrand.getName());
        data.put("desc", marketBrand.getDesc());
        data.put("floorPrice", marketBrand.getFloorPrice());
        data.put("addTime", addTimeToString);
        data.put("updateTime", updateTimeToString);
        if (!StringUtils.isEmpty(marketBrand.getPicUrl())) {
            data.put("picUrl", marketBrand.getPicUrl());
        }
        Object ok = ResponseUtil.ok(data);
        return ok;
    }
}
